package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmProject;
import com.scmt.hmzd.entity.HmTechnician;
import com.scmt.hmzd.service.IHmTechnicianService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmTechnicianMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmTechnicianServiceImpl extends ServiceImpl<HmTechnicianMapper, HmTechnician> implements IHmTechnicianService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmTechnicianMapper hmTechnicianMapper;

    @Override
    public IPage<HmTechnician> queryHmTechnicianListByPage(HmTechnician hmTechnician, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmTechnician> pageData = new Page<>(page, limit);
        QueryWrapper<HmTechnician> queryWrapper = new QueryWrapper<>();
        if (hmTechnician != null) {
            queryWrapper = LikeAllField(hmTechnician, searchVo);
        }
        IPage<HmTechnician> result = hmTechnicianMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmTechnician hmTechnician, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmTechnician> queryWrapper = new QueryWrapper<>();
        if (hmTechnician != null) {
            queryWrapper = LikeAllField(hmTechnician, null);
        }
        List<HmTechnician> list = hmTechnicianMapper.selectList(queryWrapper);
        for (HmTechnician re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("姓名", re.getName());
            map.put("图片地址", re.getCover());
            map.put("技师简介", re.getIntroduce());
            map.put("性别", re.getSex());
            map.put("地址信息", re.getAddress());
            map.put("纬度", re.getLat());
            map.put("经度", re.getLng());
            map.put("是否上钟", re.getIsWork());
            map.put("手机号", re.getMobile());
            map.put("工作照", re.getWorkImg());
            map.put("接单数量", re.getOrderNum());
            map.put("评分", re.getStar());
            map.put("微信id", re.getWxUserId());
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    @Override
    public IPage<HmTechnician> queryTechnicianListByPage(HmTechnician technician, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmProject> pageData = new Page<>(page, limit);
        return hmTechnicianMapper.queryTechnicianListByPage(technician, pageData);
    }


    /**
     * 功能描述：构建模糊查询
     *
     * @param hmTechnician 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmTechnician> LikeAllField(HmTechnician hmTechnician, SearchVo searchVo) {
        QueryWrapper<HmTechnician> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(hmTechnician.getId())) {
            queryWrapper.and(i -> i.like("hm_technician.id", hmTechnician.getId()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getName())) {
            queryWrapper.and(i -> i.like("hm_technician.name", hmTechnician.getName()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getCover())) {
            queryWrapper.and(i -> i.like("hm_technician.cover", hmTechnician.getCover()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getIntroduce())) {
            queryWrapper.and(i -> i.like("hm_technician.introduce", hmTechnician.getIntroduce()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getSex())) {
            queryWrapper.and(i -> i.like("hm_technician.sex", hmTechnician.getSex()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getAddress())) {
            queryWrapper.and(i -> i.like("hm_technician.address", hmTechnician.getAddress()));
        }
        if (hmTechnician.getLat() != null) {
            queryWrapper.and(i -> i.like("hm_technician.lat", hmTechnician.getLat()));
        }
        if (hmTechnician.getLng() != null) {
            queryWrapper.and(i -> i.like("hm_technician.lng", hmTechnician.getLng()));
        }
        if (hmTechnician.getIsWork() != null) {
            queryWrapper.and(i -> i.like("hm_technician.is_work", hmTechnician.getIsWork()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getMobile())) {
            queryWrapper.and(i -> i.like("hm_technician.mobile", hmTechnician.getMobile()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getWorkImg())) {
            queryWrapper.and(i -> i.like("hm_technician.work_img", hmTechnician.getWorkImg()));
        }
        if (hmTechnician.getOrderNum() != null) {
            queryWrapper.and(i -> i.like("hm_technician.order_num", hmTechnician.getOrderNum()));
        }
        if (hmTechnician.getStar() != null) {
            queryWrapper.and(i -> i.like("hm_technician.star", hmTechnician.getStar()));
        }
        if (StringUtils.isNotBlank(hmTechnician.getWxUserId())) {
            queryWrapper.and(i -> i.like("hm_technician.wx_user_id", hmTechnician.getWxUserId()));
        }
        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmTechnician::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        queryWrapper.lambda().and(i -> i.eq(HmTechnician::getDelFlag, 0));
        return queryWrapper;

    }
}
